import {Injectable} from '@angular/core';
import {FileInfo} from "@shared/models/FileInfo";
import {Permission} from "@shared/models/Permission";
import {api} from "@core/service/config/api";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Result} from "@shared/models/Result";
import {BBYUtils} from "@shared/utils/BBYUtils";
import {TestData} from "@shared/utils/TestData";
import {PmMessage} from "@shared/models/PmMessage";

export interface FileOptionReq {
  opera: 'copy' | 'move' | 'rename' | 'delete',
  fileList: {
    id?: string
    newName?: string,
    from?: string,
    to?: string
  }[]
}
export class SearchReq {
  /**
   * 目录id/用户id，为空时全局搜索
   */
  target:string;
  /**
   * 关键字
   */
  keyword:string;
  /**
   * 分页，默认第一页
   */
  page=1
  /**
   * 每页条数，默认10
   */
  limit=10

  constructor(target: string, keyword: string) {
    this.target = target;
    this.keyword = keyword;
  }
}

@Injectable({
  providedIn: 'root',
})
export class DiskService {

  constructor(private http: HttpClient) {
  }


  msg = "aaa";

  loadDirTest(target: string, action?: 'path' | 'id') {
    // return this.http.get(`${api.list}/${action}/?target=${target}`);
    console.log(`加载 ${target}`)
    if (target == "/" || target == '') return TestData.root;
    else return TestData.dir1;
  }


  loadDir(target: string, action: 'path' | 'id') {
    console.log(`加载 ${target},方式：${action}`)
    return this.http.get<Result>(`${api.list}/${action}/?target=${target}`, BBYUtils.createHeadersOption());
  }

  fileOption(data: FileOptionReq) {
    console.log(`文件操作：`, data)
    return this.http.post<Result>(api.opera, data, BBYUtils.createHeadersOption());
  }

  mkdir(file: FileInfo) {
    console.log(`新建文件夹：`, file)
    return this.http.post<Result>(`${api.mkdir}?parent=${file.parent}&dirname=${file.name}`, {}, BBYUtils.createHeadersOption());
  }

  editPermission(data: { ids: string[], permission?: Permission }) {
    console.log(`修改权限：`, data)
    return this.http.post<Result>(api.permission, data, BBYUtils.createHeadersOption());
  }

  download(fileId: string,directId:string) {
    console.log(`${api.download}/direct/${fileId}?directId=${directId}`)
    window.location.href=`${api.download}/direct/${fileId}?directId=${directId}`
  }

  createDownload(file: FileInfo,pass?:string) {
    return this.http.get<Result>(`${api.download}/create?fileId=${file.id}`, BBYUtils.createHeadersOption(pass))
  }

  createDownloads(preIds: string[]) {
    return this.http.get<Result>(`${api.download}/create?fileId=${preIds.join("&fileId=")}`, {headers:BBYUtils.createHeadersOption().headers})
  }
  // createVideoImg(fileId:string) {
  //   return this.http.get<Result>(`${api.download}/videoImg?fileId=${fileId}`, {headers:BBYUtils.createHeadersOption().headers})
  // }
  // createVideoImgs(preIds: string[]) {
  //   return this.http.get<Result>(`${api.download}/videoImg?fileId=${preIds.join("&fileId=")}`, {headers:BBYUtils.createHeadersOption().headers})
  // }

  viewText(file: FileInfo) {
    return this.http.get<Result>(`${api.download}/text/${file.id}?directId=${file.directId}`, BBYUtils.createHeadersOption())
  }

  search(data: SearchReq) {
    return this.http.get<Result>(BBYUtils.ObjToUrlParam(api.searchFile,data), BBYUtils.createHeadersOption())
  }

  filePath(fileId:string) {
    return this.http.get<Result>(`${api.filePath}?fileId=${fileId}`, BBYUtils.createHeadersOption())
  }
  userRoots(page: number=1) {
    return this.http.get<Result>(`${api.userRoots}?page=${page}`, BBYUtils.createHeadersOption())
  }

  fileInfo(fileId: string='') {
    return this.http.get<Result>(`${api.fileInfo}?fileId=${fileId}`, BBYUtils.createHeadersOption())
  }
  fileInfos(ids:string[]) {
    return this.http.get<Result>(`${api.fileInfos}?fileId=${ids.join('&fileId=')}`, BBYUtils.createHeadersOption())
  }

  classify(category: any) {
    return this.http.get<Result>(`${api.classify}/${category}`, BBYUtils.createHeadersOption())
  }

  addPmMessage(data: PmMessage) {
    return this.http.post<Result>(`${api.messageAdd}`,data, BBYUtils.createHeadersOption())
  }

  addOffline(target: string, url:string) {
    return this.http.post<Result>(`${api.addOffline}`, {target,url}, BBYUtils.createHeadersOption())
  }
  listOffline() {
    return this.http.get<Result>(`${api.listOffline}`, BBYUtils.createHeadersOption())
  }

  reportFile(fileId: string, message:string) {
    return this.http.post<Result>(`${api.reportFile}`, {fileId,message}, BBYUtils.createHeadersOption())
  }
}
